/*
 * ShoppingCartProduct.java
 *
 * Created on 2010-feb-26, 09:28:58
 */

package dat215projekt.gui.components;

import dat215projekt.data.IMatDataHandlerExtended;
import java.awt.Color;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import org.jdesktop.application.Action;
import se.chalmers.cse.dat215.project.ShoppingItem;

/**
 *
 * @author Administratör
 */
public class ShoppingCartProduct extends javax.swing.JPanel {

    private final ShoppingItem item;
    private static final IMatDataHandlerExtended handler = IMatDataHandlerExtended.getInstance();

    private static final Color rowStripeColor_1 = new Color(240,245,250);
    private static final Color rowStripeColor_2 = new Color(255,255,255);


    private static final NumberFormat moneyFormat;
    static {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setDecimalSeparator(',');
        symbols.setGroupingSeparator(' ');

        moneyFormat = new DecimalFormat("###,##0.00",symbols);
    }

    /** Creates new form ShoppingCartProduct */
    public ShoppingCartProduct(ShoppingItem i, int num) {
        initComponents();

        // Set smaller icons
        
        //plusButton.setIcon(getScaledIcon(plusButton.getIcon(),16,16));

        this.item = i;
        
        // Set text
        nameLabel.setText(i.getProduct().getName());
        unitSuffixLabel.setText(i.getProduct().getUnitSuffix());
        priceLabel.setText(moneyFormat.format(i.getTotal())+" kr");
        updateAmount();

        // Set background color for row-striping
        if(num % 2 == 1){
            this.setBackground(rowStripeColor_1);
        }
        else{
             this.setBackground(rowStripeColor_2);
        }
    }

    /*private Icon getScaledIcon(ImageIcon icon, int width, int height){
       // return new ImageIcon(new ImageIcon(icon).getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH));

    }*/
    private void updateAmount(){
        amountLabel.setText((int)item.getAmount()+"");
    }

    @Action
    public void addOne() {
        handler.changeShoppingItemAmount(item, 1);
        //item.setAmount(item.getAmount()+1);
        updateAmount();
    }

    @Action
    public void removeOne() {
        //item.setAmount(item.getAmount()-1);
        handler.changeShoppingItemAmount(item, -1);
        updateAmount();
    }

    @Action
    public void removeAll() {
        handler.removeFromShoppingCart(item);
       //IMatDataHandlerExtended.getInstance().getShoppingCart().removeItem(item);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        nameLabel = new javax.swing.JLabel();
        amountLabel = new javax.swing.JLabel();
        priceLabel = new javax.swing.JLabel();
        unitSuffixLabel = new javax.swing.JLabel();
        removeButton = new javax.swing.JButton();
        minusButton = new javax.swing.JButton();
        plusButton = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(dat215projekt.Dat215projektApp.class).getContext().getResourceMap(ShoppingCartProduct.class);
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        setMaximumSize(new java.awt.Dimension(32000, 64));
        setMinimumSize(new java.awt.Dimension(0, 64));
        setName("Form"); // NOI18N

        nameLabel.setFont(resourceMap.getFont("nameLabel.font")); // NOI18N
        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setName("nameLabel"); // NOI18N

        amountLabel.setFont(resourceMap.getFont("nameLabel.font")); // NOI18N
        amountLabel.setText(resourceMap.getString("amountLabel.text")); // NOI18N
        amountLabel.setName("amountLabel"); // NOI18N

        priceLabel.setFont(resourceMap.getFont("nameLabel.font")); // NOI18N
        priceLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        priceLabel.setText(resourceMap.getString("priceLabel.text")); // NOI18N
        priceLabel.setName("priceLabel"); // NOI18N

        unitSuffixLabel.setFont(resourceMap.getFont("nameLabel.font")); // NOI18N
        unitSuffixLabel.setText(resourceMap.getString("unitSuffixLabel.text")); // NOI18N
        unitSuffixLabel.setName("unitSuffixLabel"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(dat215projekt.Dat215projektApp.class).getContext().getActionMap(ShoppingCartProduct.class, this);
        removeButton.setAction(actionMap.get("removeAll")); // NOI18N
        removeButton.setIcon(resourceMap.getIcon("removeButton.icon")); // NOI18N
        removeButton.setText(resourceMap.getString("removeButton.text")); // NOI18N
        removeButton.setContentAreaFilled(false);
        removeButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        removeButton.setName("removeButton"); // NOI18N

        minusButton.setAction(actionMap.get("removeOne")); // NOI18N
        minusButton.setIcon(resourceMap.getIcon("minusButton.icon")); // NOI18N
        minusButton.setText(resourceMap.getString("minusButton.text")); // NOI18N
        minusButton.setContentAreaFilled(false);
        minusButton.setMaximumSize(new java.awt.Dimension(18, 18));
        minusButton.setMinimumSize(new java.awt.Dimension(18, 18));
        minusButton.setName("minusButton"); // NOI18N
        minusButton.setPreferredSize(new java.awt.Dimension(18, 18));

        plusButton.setAction(actionMap.get("addOne")); // NOI18N
        plusButton.setIcon(resourceMap.getIcon("plusButton.icon")); // NOI18N
        plusButton.setText(resourceMap.getString("plusButton.text")); // NOI18N
        plusButton.setToolTipText(resourceMap.getString("plusButton.toolTipText")); // NOI18N
        plusButton.setName("plusButton"); // NOI18N
        plusButton.setPreferredSize(new java.awt.Dimension(18, 18));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(plusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(minusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nameLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(amountLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(unitSuffixLabel)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 84, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(removeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(priceLabel))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(plusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                        .addComponent(minusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(nameLabel)
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(amountLabel)
                            .addComponent(unitSuffixLabel)
                            .addComponent(priceLabel)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(removeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel amountLabel;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton minusButton;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JButton plusButton;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JButton removeButton;
    private javax.swing.JLabel unitSuffixLabel;
    // End of variables declaration//GEN-END:variables

}
